package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.TicketType;
import com.yoyo.itravel.constants.MsgConstant;
import com.yoyo.itravel.dao.ITicketTypeDao;
import com.yoyo.itravel.dao.ITicketTypeDetailDao;
import com.yoyo.itravel.exceptions.TicketTypeException;
import com.yoyo.itravel.service.ITicketTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description 景点门票具体类型操作服务层接口实现类
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/18 21:55
 */
@Service
public class TicketTypeServiceImpl implements ITicketTypeService {

    @Autowired
    private ITicketTypeDao ticketTypeDao;
    @Autowired
    private ITicketTypeDetailDao ticketTypeDetailDao;

    /**
     * @return java.util.List<cn.hut.travel.bean.TicketTypeDetail>
     * @Description 查找所有的景点门票具体类型
     */
    @Override
    public List<TicketType> findAll() throws TicketTypeException {
        try {
            return ticketTypeDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new TicketTypeException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @param sightId
     * @return java.util.List<cn.hut.travel.bean.TicketType>
     * @Description 通过景点id查找该景点中的所有门票类型
     */
    @Override
    public List<TicketType> findBySightId(String sightId) throws TicketTypeException {
        try {
            return ticketTypeDao.selectBySightId(sightId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new TicketTypeException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @param sightId
     * @return BigDecimal
     * @Description 通过景点id查找景点门票具体类型的最低价
     */
    @Override
    public BigDecimal findLowestPriceBySightId(String sightId) throws TicketTypeException {
        BigDecimal lowestPrice = null;
        try {
            // 通过景点id查找该景点的所有门票类型
            List<TicketType> ticketTypes = ticketTypeDao.selectBySightId(sightId);
            if (ticketTypes == null) {
                return null;
            }
            // 遍历所有的门票类型，找到最低的具体门票类型价格
            for (TicketType ticketType : ticketTypes) {
                // 通过门票类型id查找该类型下的具体门票类型价格
                BigDecimal price = ticketTypeDetailDao.selectLowestPriceByTicketTypeId(ticketType.getTicketTypeId());
                if (price == null) {
                    // 查出来的价格为null，直接跳过此次循环
                    continue;
                }
                if (lowestPrice == null) {
                    // 如果lowestPrice为null，直接赋值
                    lowestPrice = price;
                } else {
                    // 如果lowestPrice不为null，则需要比较
                    if (lowestPrice.subtract(price).doubleValue() > 0) {
                        lowestPrice = price;
                    }
                }
            }
            // 返回最低价
            return lowestPrice;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
